import logger from '../core/logger';
import returnModel from '../model/common/returnModel';
import apiInfo from './core/apiInfo';
import apiProvider from './core/apiProvider';
import { clientBase } from './core/clientBase';

export class searchClient extends clientBase {
  constructor() {
    super();
  }

  getLives(keywords: string, index?: number): Promise<returnModel<any>> {
    var self = this;
    return new Promise((resolve) => {
      let url;
      if (index)
        url = `https://api.bilibili.com/x/web-interface/search/type?context=&platform=pc&search_type=live&cover_type=user_cover&page=${index}&keyword=${keywords}&__refresh__=true&changing=mid&highlight=1&single_column=0`;
      else
        url = `https://api.bilibili.com/x/web-interface/search/type?search_type=live&platform=pc&keyword=${keywords}&page=1`;
      this.client.getResult(url).then((res) => {
        try {
          if (res.code == 0)
            resolve(self.buildSuccessResult<any>(res));
          else
            resolve(self.buildFailedResult<any>(res.message));
        }
        catch (e) {
          logger.error(e);
          resolve(self.buildExceptionResult<any>(e));
        }
      });
    });
  }

  getUser(keywords: string, index: number): Promise<returnModel<any>> {
    var self = this;
    return new Promise((resolve) => {
      let url = `https://api.bilibili.com/x/web-interface/search/type?search_type=live_user&platform=pc&keyword=${keywords}&page=${index}&order=online&coverType=user_cover`;
      this.client.getResult(url).then((res) => {
        try {
          if (res.code == 0)
            resolve(self.buildSuccessResult<any>(res));
          else
            resolve(self.buildFailedResult<any>(res.message));
        }
        catch (e) {
          logger.error(e);
          resolve(self.buildExceptionResult<any>(e));
        }
      });
    });
  }

  getLiving(keywords: string, index: number): Promise<returnModel<any>> {
    var self = this;
    return new Promise((resolve) => {
      let url = `https://api.bilibili.com/x/web-interface/search/type?search_type=live&platform=pc&keyword=${keywords}&page=${index}`;
      this.client.getResult(url).then((res) => {
        try {
          if (res.code == 0)
            resolve(self.buildSuccessResult<any>(res));
          else
            resolve(self.buildFailedResult<any>(res.message));
        }
        catch (e) {
          logger.error(e);
          resolve(self.buildExceptionResult<any>(e));
        }
      });
    });
  }

  getMovies(keywords: string, index: number): Promise<returnModel<any>> {
    var self = this;
    return new Promise((resolve) => {
      let url = `https://api.bilibili.com/x/web-interface/search/type?context=&platform=pc&search_type=media_ft&page=${index}&order=&keyword=${keywords}&category_id=&__refresh__=true&highlight=1&single_column=0`;
      this.client.getResult(url).then((res) => {
        try {
          if (res.code == 0)
            resolve(self.buildSuccessResult<any>(res));
          else
            resolve(self.buildFailedResult<any>(res.message));
        }
        catch (e) {
          logger.error(e);
          resolve(self.buildExceptionResult<any>(e));
        }
      });
    });
  }

  getBangmis(keywords: string, index: number): Promise<returnModel<any>> {
    var self = this;
    return new Promise((resolve) => {
      let url = `https://api.bilibili.com/x/web-interface/search/type?context=&platform=pc&search_type=media_bangumi&page=${index}&order=&keyword=${keywords}&category_id=&__refresh__=true&highlight=1&single_column=0`;
      this.client.getResult(url).then((res) => {
        try {
          if (res.code == 0)
            resolve(self.buildSuccessResult<any>(res));
          else
            resolve(self.buildFailedResult<any>(res.message));
        }
        catch (e) {
          logger.error(e);
          resolve(self.buildExceptionResult<any>(e));
        }
      });
    });
  }

  getActivities(keywords: string, index: number): Promise<returnModel<any>> {
    var self = this;
    return new Promise((resolve) => {
      let url = `https://api.bilibili.com/x/web-interface/search/type?context=&platform=pc&search_type=topic&page=${index}&order=&keyword=${keywords}&category_id=&__refresh__=true&highlight=1&single_column=0`;
      this.client.getResult(url).then((res) => {
        try {
          if (res.code == 0)
            resolve(self.buildSuccessResult<any>(res));
          else
            resolve(self.buildFailedResult<any>(res.message));
        }
        catch (e) {
          logger.error(e);
          resolve(self.buildExceptionResult<any>(e));
        }
      });
    });
  }

  getUsers(keywords: string, order: string, type: string, index: number): Promise<returnModel<any>> {
    var self = this;
    return new Promise((resolve) => {
      let url = `https://api.bilibili.com/x/web-interface/search/type?context=&platform=pc&search_type=bili_user&page=${index}&keyword=${keywords}${order}${type}&__refresh__=true&changing=mid&highlight=1&single_column=0&category_id=`;
      this.client.getResult(url).then((res) => {
        try {
          if (res.code == 0)
            resolve(self.buildSuccessResult<any>(res));
          else
            resolve(self.buildFailedResult<any>(res.message));
        }
        catch (e) {
          logger.error(e);
          resolve(self.buildExceptionResult<any>(e));
        }
      });
    });
  }

  getArticles(keywords: string, order: string, region: string, index: number): Promise<returnModel<any>> {
    var self = this;
    return new Promise((resolve) => {
      let url = `https://api.bilibili.com/x/web-interface/search/type?context=&platform=pc&search_type=article&page=${index}&order=${order}&keyword=${keywords}&category_id=${region}&__refresh__=true&highlight=1&single_column=0`;
      this.client.getResult(url).then((res) => {
        try {
          if (res.code == 0)
            resolve(self.buildSuccessResult<any>(res));
          else
            resolve(self.buildFailedResult<any>(res.message));
        }
        catch (e) {
          logger.error(e);
          resolve(self.buildExceptionResult<any>(e));
        }
      });
    });
  }

  getVideos(keywords: string, page: number, areaCondition: string, typeCondition: string): Promise<returnModel<any>> {
    var self = this;
    return new Promise((resolve) => {
      let url = `https://api.bilibili.com/search?_device=android&appkey=${apiProvider.AndroidKey.appkey}&build=${apiProvider.Build5}&keyword=${keywords}&main_ver=v3&page=${page}&pagesize=20&platform=android&search_type=video&source_type=0${areaCondition}${typeCondition}`;
      this.client.getResult(url).then((res) => {
        try {
          if (res.code == 0)
            resolve(self.buildSuccessResult<any>(res));
          else
            resolve(self.buildFailedResult<any>(res.message));
        }
        catch (e) {
          logger.error(e);
          resolve(self.buildExceptionResult<any>(e));
        }
      });
    });
  }
}
export default new searchClient();